<?php
namespace app\mouth\controller;
use app\mouth\common\Api;
use app\mouth\model\UserModel;
use app\mouth\controller\CircleUserApi;
use lib\wxBizDataCrypt;
use think\Controller;
/**
 * 个人中心
 * Class UserApi
 * @package app\mouth\controller
 */
class UserApi extends Api{

    /**
     * 会员中心  ---个人信息
     * @return false|string
     */
    public function index(){
        $user_nus = (new UserModel)->index($this->openid);
        $this->add($user_nus);
    }

    /**
     * 技事本添加
     */
    public function Bookkeeping(){
        $this->add((new UserModel)->content(input(),$this->openid));
    }
    /**
     * 技事本修改
     */
    public function Bookkeepingup(){
       $array = (new UserModel)->contents(input(),$this->openid);
       if (is_array($array)){
           return $this->jsons('200','成功',$array);die;
       }
        $this->add($array);
    }
    /**
     * 各种保险
     * openid 用户id
     * vu_id 技事本id
     */
    public function insurance(){
       $this->add((new UserModel)->protect($this->openid,input('vu_id')));
    }
    /**
     * 显示身份证件
     */
    public function adminindex(){
        return $this->jsons('200','成功',(new UserModel)->appear($this->openid));
    }

    public function surface(){
        return $this->jsons('200','成功',[]);
    }
    /**
     * 添加身份证件
     */
    public function certificates(){
        if (empty(request()->file())){
            return $this->jsons('40003','失败',[]);
        }
        if (empty(request()->file('idcard'))){
            if (empty(request()->file('license'))){
                if (empty(request()->file('travel'))){
                }else{
                    $add['travel'] = (new CircleUserApi)->add(request()->file('travel'));
                }
            }else{
                $add['license'] = (new CircleUserApi)->add(request()->file('license'));
            }
        }else{
            $add['idcard'] = (new CircleUserApi)->add(request()->file('idcard'));
        }
        (new UserModel)->certificatesadd($add,$this->openid);
    }
    /**
     * 添加手机号
     * @return int|void
     */
    public function decrypt(){
        $appid = 'wxdb29abf88a2ab5e9';
        $sessionKey = $this->session_key;
        $encryptedData = input('encryptedData');
        $iv=input('iv');
        $data = '';
        $pc = new wxBizDataCrypt($appid, $sessionKey);
        $errCode = $pc->decryptData($encryptedData, $iv, $data );
        if ($errCode == 0) {
            $value = json_decode($data,true);
            (new UserModel)->addMobileNumber($this->openid,$value['phoneNumber']);
            return $data;
        } else {
            return $errCode;
        }
    }
    /**
     * 我的车辆
     * @param   $openid    //车辆id
     */
    public function vehicle(){
            $user_nus = (new UserModel)->userOpenid($this->openid);
            if ($user_nus){
                return $this->jsons('200','成功',$user_nus);
            }
        return $this->jsons('100','暂无车辆,请添加!!!',[]);
    }
    /**
     * 我的卡卷
     * @param   $id  //用户id
     */
    public function discount(){
        $uservolume = (new UserModel)->discount($this->user_id,(int)input('type'));
        if ($uservolume){
            return $this->jsons('200','成功',$uservolume);
        }
        return $this->jsons('100','暂无卡卷',[]);
    }
    /**
     * 我的订单
     * @param   $openid
     * evaluate   状态 1未支付，2已支付，3 已完成，4 已退款  5待评价
     * 空为全部状态
     */
    public function order(){
        if (input('evaluate') == 0){
            $evaluate = '';
        }else{
            $evaluate = input('evaluate');
        }
        $myorder = (new UserModel)->volume($this->openid,$evaluate);
        $this->jsons('40006','成功',$myorder);
    }

    /**
     * 我的服务
     * @param   $openid
     */
    public function service(){

    }
    /**
     * 问答首页
     * 问答分类首页
     * @param  $qc_id
     * 分类id
     * @param   $openid
     */
    public function qa_question(){
        $qc_id = (int)input('qc_id');
        $this->jsons('40006','成功',(new UserModel)->qa_question($qc_id));
    }
    /**
     *提问
     */
    public function putquestionsto(){
        $date= (new UserModel)->putquestionsto(input(),$this->nickname,$this->portrait);
        if ($date === true){
            $this->jsons('40006','成功',[]);
        }
        $this->jsons('40003','失败',$date);
    }
    /**
     * 我的提问
     */
    public function myquestion(){
        $this->jsons('40006','成功',(new UserModel)->myquestion($this->openid));
    }
    /**
     * 问题详情
     * @param $id
     * 问题id
     */
    public function questiondetails(){
        $this->jsons('40006','成功',(new UserModel)->questiondetails((int)input('qq_id')));
    }
    /**
     * 我的回答
     */
    public function myanswer(){
        $this->jsons('40006','成功',(new UserModel)->myanswer($this->openid));
    }

    /**
     * 回答
     */
    public function questionanswering(){
        $date= (new UserModel)->questionanswering(input(),$this->openid,$this->nickname,$this->portrait);
        if ($date === true){
            $this->jsons('40006','成功',[]);
        }
        $this->jsons('40003','失败',$date);
    }
    /**
     * 删除
     * @param $id
     * 问题id
     */
    public function deletequestions(){
        $this->jsons('40006','成功',(new UserModel)->deletequestions((int)input('qq_id')));
    }
}